//
//  splitViewVideoViewController.m
//  iSeeColor
//
//  Created by narbeh shahnazarian on 12-11-17.
//  Copyright (c) 2012 Wyatt Gosling. All rights reserved.
//

#import "splitViewVideoViewController.h"
#import "colorBlindEyesFilter.h"

@interface splitViewVideoViewController () <UIActionSheetDelegate>
{
      // a video camera object
      GPUImageVideoCamera *splitView_VideoCamera;
    
      // a filter object
      GPUImageFilter *filter;
 
}


@end

@implementation splitViewVideoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //create our view
    CGRect mainScreenFrame = [[UIScreen mainScreen] bounds];
    UIView *primaryView = [[UIView alloc] initWithFrame:mainScreenFrame];
    primaryView.backgroundColor = [UIColor blackColor];
    self.view = primaryView;
    
 
    //instatiate our GPUImageVideoCamera
    splitView_VideoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    splitView_VideoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
     
    //grab dimensions of view
    CGFloat Width = round(self.view.frame.size.width);
    CGFloat Height = round(self.view.frame.size.height);
    CGFloat halfHeight = round(self.view.frame.size.height)/2.0;
  // NSLog(@"height is %f", Height);
 //  NSLog(@"Width is %f", Width);
   
    //our two half views:
    //view1: a normal camera view
    //view2: a camera view of approximatly what a protanope will see in real life.
    GPUImageView *view1, *view2;
    
    //we test here if the device is an iPhone since we have to make different frame allocations depending on the device
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
    view1 = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, -25.0, Width, Height)];
    view2 = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, halfHeight-45.0, Width, Height-40.0)];
    }//end if
    
    //device is an iPad
    else
    {
    view1 = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, Width, Height)];
    view2 = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, halfHeight, Width, Height)];
    }
   // NSLog(@"after view2 is created");
    //we now add the subviews and send view1 to the back so that the navigation controller will be visiable on screen
    [self.view addSubview:view1];
    [self.view addSubview:view2];
    [self.view sendSubviewToBack:view1];
    
    
    
    // Setup initial camera filter with the shader.
    filter = [[colorBlindEyesFilter alloc] init];//[[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"lifeThroughAProtanopesEyesFilter"];
                                                                    //change this string to the filter you want.
                                                                    //ie: lifeThroughAProtanopesEyesFilter or Daltonize
     
  //  [filter forceProcessingAtSize:view2.sizeInPixels];
    
    //We must chain our views and filters as follows:  splitView_VideoCamera -> view1
    //                                                 splitView_VideoCamera -> filter -> view2  
    [splitView_VideoCamera addTarget:view1];
    [splitView_VideoCamera addTarget:filter];
    [filter addTarget:view2];
    
    //we begin the video camera capture
    [splitView_VideoCamera startCameraCapture];
//    NSLog(@"method finished");

	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
   
}

- (void)viewWillDisappear:(BOOL)animated
{
    // Note: We need to stop camera capture before the view goes off the screen in order to prevent a crash from the camera still sending frames
    [splitView_VideoCamera stopCameraCapture];
    
    //we remove all targets from the filter and splitView_Camera
    [splitView_VideoCamera removeAllTargets];
    [filter removeAllTargets];
    //[test removeAllTargets];
      //  NSLog(@"i got to this point");
    [super viewWillDisappear:animated];
}

@end
